/*
 * Copyright (c) 2023. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
 * Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
 * Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna.
 * Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
 * Vestibulum commodo. Ut rhoncus gravida arcu.
 */

package com.gzdh.device.system.util;



import com.gzdh.device.comm.util.oConvertUtils;
import com.gzdh.device.system.entity.SysPermission;
import com.gzdh.device.system.model.PermissionIdModel;
import com.gzdh.device.system.model.SysPermissionTreeModel;

import java.util.ArrayList;
import java.util.List;

public class FindsPermissionChildrenUtil {


    public static List<SysPermissionTreeModel> wrapTreeDataToTreeList(List<SysPermission> recordList) {
        // 在该方法每请求一次,都要对全局list集合进行一次清理
        //idList.clear();
        List<PermissionIdModel> idList = new ArrayList<PermissionIdModel>();
        List<SysPermissionTreeModel> records = new ArrayList<>();
        for (int i = 0; i < recordList.size(); i++) {
            SysPermission depart = recordList.get(i);
            records.add(new SysPermissionTreeModel(depart));
        }
        List<SysPermissionTreeModel> tree = findChildren(records, idList);
        setEmptyChildrenAsNull(tree);
        return tree;
    }







    /**
     * 获取 DepartIdModel
     *
     * @param recordList
     * @return
     */
    public static List<PermissionIdModel> wrapTreeDataToDepartIdTreeList(List<SysPermission> recordList) {
        // 在该方法每请求一次,都要对全局list集合进行一次清理
        //idList.clear();
        List<PermissionIdModel> idList = new ArrayList<PermissionIdModel>();
        List<SysPermissionTreeModel> records = new ArrayList<>();
        for (int i = 0; i < recordList.size(); i++) {
            SysPermission depart = recordList.get(i);
            records.add(new SysPermissionTreeModel(depart));
        }
        findChildren(records, idList);
        return idList;
    }

    /**
     * queryTreeList的子方法 ====2=====
     * 该方法是找到并封装顶级父类的节点到TreeList集合
     */
    private static List<SysPermissionTreeModel> findChildren(List<SysPermissionTreeModel> recordList,
                                                             List<PermissionIdModel> departIdList) {

        List<SysPermissionTreeModel> treeList = new ArrayList<>();
        for (int i = 0; i < recordList.size(); i++) {
            SysPermissionTreeModel branch = recordList.get(i);
            if (oConvertUtils.isEmpty(branch.getParentId())) {
                treeList.add(branch);
                PermissionIdModel departIdModel = new PermissionIdModel().convert(branch);
                departIdList.add(departIdModel);
            }
        }
        getGrandChildren(treeList, recordList, departIdList);

        //idList = departIdList;
        return treeList;
    }

    /**
     * queryTreeList的子方法====3====
     * 该方法是找到顶级父类下的所有子节点集合并封装到TreeList集合
     */
    private static void getGrandChildren(List<SysPermissionTreeModel> treeList, List<SysPermissionTreeModel> recordList, List<PermissionIdModel> idList) {

        for (int i = 0; i < treeList.size(); i++) {
            SysPermissionTreeModel model = treeList.get(i);
            PermissionIdModel idModel = idList.get(i);
            for (int i1 = 0; i1 < recordList.size(); i1++) {
                SysPermissionTreeModel m = recordList.get(i1);
                if (m.getParentId() != null && m.getParentId().equals(model.getId())) {
                    model.getChildren().add(m);
                    PermissionIdModel dim = new PermissionIdModel().convert(m);
                    idModel.getChildren().add(dim);
                }
            }
            getGrandChildren(treeList.get(i).getChildren(), recordList, idList.get(i).getChildren());
        }

    }


    /**
     * queryTreeList的子方法 ====4====
     * 该方法是将子节点为空的List集合设置为Null值
     */
    private static void setEmptyChildrenAsNull(List<SysPermissionTreeModel> treeList) {

        for (int i = 0; i < treeList.size(); i++) {
            SysPermissionTreeModel model = treeList.get(i);
            if (model.getChildren().size() == 0) {
                model.setChildren(null);
                model.setLeaf(true);
            } else {
                setEmptyChildrenAsNull(model.getChildren());
                model.setLeaf(false);
            }
        }
        // sysDepartTreeList = treeList;
    }
}
